<template>
  <a-modal
    v-model="show"
    :centered="true"
    :maskClosable="false"
    width=70%
    :title="fromName"
    ok-text="确认" cancel-text="取消"
    @cancel="onClose" @ok="handleSubmit">
    <!-- 表单区域 -->
    <fm-generate-antd-form
      :data="jsonData"
      :remote="remoteFuncs"
      :value="editData"
      :remote-option="dynamicData"
      ref="generateForm"
      v-if="ApplyFormVisiable"
      @form-change="handleFormChange"
    >
    </fm-generate-antd-form>
    <candidate-users ref="CandidateUsers" @setCandidateUser="setCandidateUser"></candidate-users>
  </a-modal>
</template>
<script>

import CandidateUsers from './CandidateUsers'

export default {
  name: 'ViewForm',
  components: {CandidateUsers},
  props: {
    ApplyFormVisiable: {
      default: false
    },
    username: {
      require: true
    }
  },
  data () {
    let reqGet = this.$get // 避免在回调函数中使用this
    return {
      candidateUsersParams: {
        sequenceId: '',
        candidateUser: ''
      },
      fromName: '',
      jsonData: {},
      editData: {},
      remoteFuncs: {
        dictFunc (resolve, params) {
          reqGet('dict/getField?field=' + params).then((r) => {
            resolve(r.data)
          })
        },
        getUsers (resolve, params) {
          reqGet('user/getAllUsers').then((r) => {
            resolve(r.data)
          })
        },
        getDepts (resolve, params) {
          reqGet('dept/getAllDepts').then((r) => {
            resolve(r.data)
          })
        },
        getBizDict (resolve, params) {
          let param = params.split(',')
          reqGet('bizModel/getModelDict/' + param[0].trim() + '/' + param[1].trim()).then((r) => {
            resolve(r.data)
          })
        }
      },
      dynamicData: {
      },
      applyData: {}
    }
  },
  computed: {
    show: {
      get: function () {
        return this.ApplyFormVisiable
      },
      set: function () {
      }
    }
  },
  methods: {
    handleFormChange (values) {
      let fieldName = Object.keys(values)[0]
      let fieldData = Object.values(values)[0]
      if (fieldName.substring(0, 3) === 'exl') {
        this.$get('/bizModel/getModelDictDetail/' + fieldData).then((r) => {
          this.editData = r.data
        })
      }
    },
    onClose () {
      this.editData = {}
      this.$emit('close')
    },
    setFormValues (data) {
      this.applyData = data
      this.fromName = data.formTitle
      this.jsonData = JSON.parse(data.formJson)
    },
    setCandidateUser (user, sequenceId) {
      this.candidateUsersParams.candidateUser = user
      this.launchProcess()
      this.$refs.CandidateUsers.closeModal()
    },
    handleSubmit () {
      this.$get('process/launch/candidateUsers?procDefKey=' + this.applyData.processkey).then((r) => {
        if (r.data.ifShowSetCandidateUser) {
          this.$refs.CandidateUsers.setUsers(r.data.userList)
          this.$refs.CandidateUsers.showModal()
        } else {
          this.launchProcess()
        }
      })
    },
    launchProcess () {
      this.$refs.generateForm.getData().then(data => {
        let formdata = JSON.stringify(data)
        this.$post('process/launch', {
          applicant: this.username,
          processDefinitionKey: this.applyData.processkey,
          flowData: formdata,
          nextAssignee: this.candidateUsersParams.candidateUser}).then((r) => {
          this.loading = false
          this.$emit('success')
          this.editData = {}
        })
        this.loading = true
      }).catch(e => {
        this.loading = false
      })
    }
  }
}
</script>
<style lang="scss">
  .dynamic-form-non-display {
    display: none;
  }
</style>
